package axht.trade.android.listview;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * 显示复杂的列表
 */
public class MyAdapter extends BaseAdapter {


    private List<Menu> dataList;
    private Context contextView;

    public MyAdapter() {
    }

    public MyAdapter(List<Menu> data, Context contextView) {
        this.dataList = data;
        this.contextView = contextView;
    }


    /**
     * 返回集合数据的数量
     *
     * @return
     */
    @Override
    public int getCount() {
        return dataList.size();
    }

    /**
     * 返回指定下标对应的数据对象
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * 返回指定下标对应的item的View对象
     *
     * @param position    下标
     * @param convertView 可复用的缓存Item视图对象，前n+1个为null
     * @param parent      ListView对象
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            Log.e("TAG", "getView() position=" + position + " convertView=" + convertView);
            convertView = View.inflate(contextView, R.layout.simple_item, null);
        }

        //根据position设置对应的数据
        //得到当前行的数据对象
        Menu menu = dataList.get(position);
        ImageView imageView = convertView.findViewById(R.id.iv_item_icon);
        TextView nameTV = convertView.findViewById(R.id.tv_item_name);
        TextView contentTV = convertView.findViewById(R.id.tv_item_content);
        //设置数据
        imageView.setImageResource(menu.getIcon());
        nameTV.setText(menu.getName());
        contentTV.setText(menu.getContent());
        return convertView;
    }
}
